package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 切面类:为目标方法进行功能增强的类
 */
@Component
@Aspect
public class LogAspect {
    /**
     * 1.@Before注解:前置通知 - 在目标方法调用之前执行;
     */
    @Before("execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public void beforeMethod(JoinPoint joinPoint){
        /**
         * 1.JoinPoint :连接点参数;
         * 2.获取方法名:joinPoint.getSignature().getName();
         * 3.获取目标方法参数:joinPoint.getArgs();
         */
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("1-前置通知:"+name+"方法执行,参数为:"+args);
    }

    /**
     * 2.afterReturning注解:返回通知 - 目标方法成功返回后执行
     * @param joinPoint
     */
    @AfterReturning(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))",returning = "r")
    public void afterReturningMethod(JoinPoint joinPoint,Object r){
        String name = joinPoint.getSignature().getName();
        System.out.println("2-返回通知:"+name+"方法结束,结果为:"+r);
    }
}
